package com.junqson.je.content.controller;

import com.junqson.je.common.UserHolder;
import com.junqson.je.common.annotation.LoginRequire;
import com.junqson.je.common.model.ApiResult;
import com.junqson.je.content.model.PostModel;
import com.junqson.je.content.model.dto.PostDTO;
import com.junqson.je.content.model.vo.ColumnWithTotal;
import com.junqson.je.content.model.vo.PostContentVo;
import com.junqson.je.content.model.vo.PostListVo;
import com.junqson.je.content.service.PostService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Descriptions
 * @Company
 * @Author Junqson
 * @Date 2020/2/11 9:38
 * @Version 1.0
 */
@RestController
@RequestMapping("post")
public class PostController {
    @Autowired
    PostService postService;

    /**
     * 发帖
     */
    @LoginRequire
    @RequestMapping("add")
    public ApiResult addPost(PostDTO dto) {
        if (dto == null) {
            return ApiResult.error("dto is null");
        }
        if (StringUtils.isEmpty(dto.getTitle()) || StringUtils.isEmpty(dto.getContent()) || StringUtils.isEmpty(dto.getSection())) {
            return ApiResult.error("标题/内容/板块不能为空");
        }
        int userId = UserHolder.get().getUserId();
        postService.addPost(dto, userId);
        return ApiResult.success();
    }


    /**
     * 用户删帖
     */
    @LoginRequire
    @RequestMapping("delete")
    public ApiResult deletePost(Long postId) {
        if (postId == null) {
            return ApiResult.error("post id is null");
        }
        PostModel model = postService.getPostModelById(postId);
        if (model == null) {
            return ApiResult.error("帖子不存在");
        }
        int userId = UserHolder.get().getUserId();
        if (userId != model.getUserId()) {
            return ApiResult.error("无权操作");
        }
        postService.deletePost(model);
        return ApiResult.success();
    }

    /**
     * 管理员删帖
     */
    @RequestMapping("admin/del")
    public ApiResult adminDeletePost(Long postId) {
        PostModel model = postService.getPostModelById(postId);
        if (model != null) {
            postService.deletePost(model);
        }
        return ApiResult.success();
    }

    /**
     * 按板块分页查询帖子
     */
    @RequestMapping("pageBySection")
    public ApiResult pageBySection(String section, Integer page, Integer size) {
        if (StringUtils.isEmpty(section)) {
            return ApiResult.error("section is null");
        }
        ColumnWithTotal models = postService.pagePostBySection(section, page, size);
        return ApiResult.success(models);
    }

    /**
     * 获取帖子内容
     */
    @RequestMapping("content")
    public ApiResult getPostContent(Long postId) {
        if (postId == null) {
            return ApiResult.error("post id is null");
        }
        PostContentVo vo = postService.getPostContent(postId);
        return ApiResult.success(vo);
    }

}
